﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.TuanHocService;
namespace eTMS.Controllers{
public class TuanHocControllerBase : CommonController{
	public TuanHocControllerBase()
		: base()
	{	
		this.dao = new TuanHocServiceClient();
	}
	~TuanHocControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private TuanHocEntity oTuanHoc;	
	public TuanHocEntity TuanHocObject{
		get{
			return oTuanHoc;
		}
		set{
			oTuanHoc=value;
		}
	}
	protected TuanHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSTuanHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSTuanHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oTuanHoc==null)return false;
		bool result=true;
		try{
			oTuanHoc = dao.SaveRet(oTuanHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListTuanHoc.Add(oTuanHoc);
		}
		return result;
	}
	public bool Update(){
		if(oTuanHoc==null)return false;
		return dao.Update(oTuanHoc);
	}
	public bool Delete(){
		if(oTuanHoc==null)return false;
		bool retvalue = dao.Delete(oTuanHoc);
		if(retvalue){			
			_ListTuanHoc.Remove(oTuanHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 TuanHoc_ID){
		bool retvalue = dao.DeleteByID(TuanHoc_ID);		
		if(retvalue){
			TuanHocEntity oDeleted=FindTuanHoc(TuanHoc_ID);
			if(oDeleted!=null){
				_ListTuanHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oTuanHoc = new TuanHocEntity();
	}
	//Check if exists MaTuanHoc
	public bool CheckExistsMaTuanHoc(object value){	
		return dao.SelectOneByMaTuanHoc(value.ToString()).MaTuanHoc!=null;
    }    
    //Copy data from data row in grid to object
    public TuanHocEntity MapDataRowToObject(DataRow row){
		TuanHocEntity o = new TuanHocEntity();
		 o.MaTuanHoc=Convert.ToString(row["MaTuanHoc"]);
		 o.TenTuanHoc=Convert.ToString(row["TenTuanHoc"]);
		 if(!row.IsNull("ThuTu"))
			o.ThuTu=ConvertInt(row["ThuTu"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("TuanHoc_ID"))
		    o.TuanHoc_ID=(Int32)(row["TuanHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(TuanHocEntity o,DataRow row){
		row["MaTuanHoc"]=o.MaTuanHoc;
		row["TenTuanHoc"]=o.TenTuanHoc;
		row["ThuTu"]=o.ThuTu;
		row["GhiChu"]=o.GhiChu;
		row["TuanHoc_ID"]=o.TuanHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.TuanHocObject,row);
    }
	public void LoadObject(Int32 TuanHoc_ID){
		TuanHocEntity oLoad=FindTuanHoc(TuanHoc_ID);
		if(oLoad==null){
			oTuanHoc=this.LoadTuanHoc(TuanHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oTuanHoc.IsNullTuanHoc_ID;
			if(!bNotIsNull){
				this.ListTuanHoc.Add(oTuanHoc);
			}
		}
		else{
			oTuanHoc=oLoad;
		}
	}
	#region CachedList TuanHoc
	private static List<TuanHocEntity> _ListTuanHoc=new List<TuanHocEntity>();
    public List<TuanHocEntity>ListTuanHoc{
		get{
			return _ListTuanHoc;
		}
	}
	public static List<TuanHocEntity>ListTuanHocStatic{
		get{
			return _ListTuanHoc;
		}
	}
	public TuanHocEntity FindTuanHoc(Int32 TuanHoc_ID){
		TuanHocEntity oResult=this.ListTuanHoc.Find(delegate(TuanHocEntity oFind){
			bool result=true; 
			result&=oFind.TuanHoc_ID.Equals(TuanHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllTuanHoc(bool clearCache){
		if(_ListTuanHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		TuanHocServiceClient dao = new TuanHocServiceClient();
		_ListTuanHoc=new List<TuanHocEntity>(dao.SelectAll());
		dao.Close();
	}
	public TuanHocEntity LoadTuanHoc(Int32 TuanHoc_ID){			
		TuanHocEntity o=dao.SelectOne(TuanHoc_ID);		
		return o;
	}
	#endregion
}
}
